//
//  AppDelegate.swift
//  Sina
//
//  Created by 管观荣 on 15/9/21.
//  Copyright © 2015年 CHer. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
//        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = defaultRootViewControler()
        window?.makeKeyAndVisible()
        
         setupAppearance()
        
        // 添加通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootViewController:", name: WBSwitchRootViewControllerNotification, object: nil)
        
        return true
    }
    /// 切换控制器
    @objc private func switchRootViewController(notification: NSNotification) {
        print("切换控制器")
        
        // 判断通知中的对象是否为nil
        let vc = (notification.object != nil) ? Welcome() : MainViewController()
        
        window?.rootViewController = vc
    }
    /// 返回启动默认的控制器 － 多态
    private func defaultRootViewControler() -> UIViewController {
        
        // 1. 判断用户是否登录
        if UserAccountViewModel.sharedAccountViewModel.userLogon {
            // 如果登录，判断是否有新版本
            return isNewVersion() ? NewFeatureViewController() : Welcome()
        }
        
        // 2. 没有登录，返回主界面
        return MainViewController()
    }
    
    /// 判断是否有新版本
    private func isNewVersion() -> Bool {
        
        // 1. 获取当前的版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let version = Double(currentVersion)!
        print(version)
        
        // 2. 获取`之前`的版本 － 将之前的版本保存在沙盒中
        let sandBoxVersionKey = "sandBoxVersionKey"
        // 如果不存在 key，会返回 0.0
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandBoxVersionKey)
        print("当前沙盒版本 - \(sandBoxVersion)")
        
        // 3. 将当前版本保存至沙盒
        NSUserDefaults.standardUserDefaults().setDouble(version, forKey: sandBoxVersionKey)
//        [NSUserDefaults.standardUserDefaults()] synchronize;
        // 4. 返回两个版本的比较结果
        return version > sandBoxVersion
    }


    /// 设置全局外观
    private func setupAppearance() {
        // 全局外观一经设置，全局有效，要尽量早的设置
        // 在商业应用中，全局外观会写在 appDelegate 中
        UINavigationBar.appearance().tintColor = WBAppearanceColor
        UITabBar.appearance().tintColor = WBAppearanceColor
    }


    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

